package com.jcfk.system.service;

import cn.hutool.core.util.StrUtil;
import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jcfk.common.business.service.BaseService;
import com.jcfk.common.core.utils.BeanUtils;
import com.jcfk.common.security.utils.SecurityUtils;
import com.jcfk.common.web.domain.DataPage;
import com.jcfk.common.web.domain.RetResult;
import com.jcfk.system.dao.SysUserAccountDao;
import com.jcfk.system.domain.dto.SysUserAccountDTO;
import com.jcfk.system.domain.po.SysUserAccount;
import com.jcfk.system.domain.query.SysUserAccountQuery;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

import static com.jcfk.common.core.utils.BeanUtils.convertList;

/**
 * 用户关联第三方平台的id
 *
 * @author awei
 * @email 522891237@gqq.com
 * @date 2024-05-14
 */
@Service
public class SysUserAccountService extends BaseService<SysUserAccountDao,SysUserAccount> {

    /**
     * SysUserAccount分页查询
     *
     * @param page  分页设置
     * @param query 查询条件
     * @return 分页查询结果
     */
    public DataPage<SysUserAccountDTO> queryByPage(DataPage<SysUserAccountDTO> page, SysUserAccountQuery query) {
        // 分页查询
        page = this.baseMapper.queryByPage(page, query);
        return page;
    }


        /**
     * 判断SysUserAccount是已经存在
     *
     * @param dto
     * @return
     */
    public RetResult<String> isValid(SysUserAccountDTO dto) {
        QueryWrapper<SysUserAccount> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("deleted", 0);
                queryWrapper.eq("user_id", dto.getUserId());
        if (!StringUtils.isEmpty(dto.getId())) queryWrapper.ne("id", dto.getId());
        Integer count = this.count(queryWrapper);

        if (count > 0) return RetResult.fail("用户id已存在");
                                                            
        return RetResult.ok();
    }

    /**
     * 插入用户关联第三方平台的id
     *
     * @param dto 插入对象
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public void insert(SysUserAccountDTO dto) {

        // 保存
        SysUserAccount item = new SysUserAccount();
        BeanUtils.copyProperties(dto, item);
        this.setDefaultValue(item);

        super.save(item);
    }

    /**
     * 更新用户关联第三方平台的id
     *
     * @param dto 更新对象
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public void update(SysUserAccountDTO dto) {

        // 保存
        SysUserAccount item = new SysUserAccount();
        BeanUtils.copyProperties(dto, item);

        // 默认数据
        this.setDefaultValue(item);

        super.updateById(item);
    }

    /**
     * 根据批量主键删除用户关联第三方平台的id(逻辑删除)
     *
     * @param dto
     * @return bool
     */
    @Transactional(rollbackFor = Exception.class)
    public Boolean logicDeleteList(SysUserAccountDTO dto) {

        SysUserAccount item = new SysUserAccount();

        this.setDefaultValue(item);
        item.setDeleted(1);

        QueryWrapper<SysUserAccount> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id", dto.getIds());

        return this.update(item, queryWrapper);
    }

    /**
     * 设置添加默认值
     *
     * @param item 添加对象
     */
    public void setDefaultValue(SysUserAccount item) {
        if (StrUtil.isEmpty(item.getCreatedUserId())) {
            item.setCreatedUserId(SecurityUtils.getUserId());
            item.setCreatedUserName(SecurityUtils.getUserName());
            item.setCreateTime(new Date());
        }
        item.setLastUpdatedUserId(SecurityUtils.getUserId());
        item.setLastUpdateTime(new Date());
        item.setLastUpdatedUserName(SecurityUtils.getUserName());
        item.setDeleted(0);

    }

    /**
     * 根据主键获取用户关联第三方平台的id信息
     *
     * @param id 主键
     * @return StandardCodeDTO
     */
    public SysUserAccountDTO get(String id) {
        // 查询数据
        SysUserAccount item = this.baseMapper.selectById(id);
            SysUserAccountDTO dto = new SysUserAccountDTO();
        try {
            BeanUtils.copyProperties(item, dto);
        } catch (RuntimeException e) {
            throw e;
        }

        return dto;
    }

    

    /**
     * 根据条件获取单表信息列表
     *
     * @param query 查询条件
     * @return List<SysUserAccountDTO>
     */
    public List<SysUserAccountDTO> getList(SysUserAccountQuery query) {
        // 查询条件
        QueryWrapper<SysUserAccount> qw = new QueryWrapper<>();
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                // 查询数据
        List<SysUserAccount> list = super.list(qw);

        return convertList(list, SysUserAccountDTO.class);
    }
}

